//
//  LWH_ViewControllerForCollect.m
//  ProjectB_
//
//  Created by dllo on 16/3/28.
//  Copyright © 2016年 项目B. All rights reserved.
//

#import "LWH_ViewControllerForCollect.h"    //C

#import "EveryDayTableViewCell.h"   //V
#import "ContentScrollView.h"
#import "ContentView.h"
#import "rilegouleView.h"
#import "CustomView.h"
#import "ImageContentView.h"

#import "EveryDayModel.h"   //M

@interface LWH_ViewControllerForCollect ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *arrayForCellData;
@property (nonatomic ,strong) UIImageView *image;
@property (nonatomic ,strong) UILabel *label;

@property (nonatomic, strong) UITableView *tableView;
@end

@implementation LWH_ViewControllerForCollect

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // self.hidesBottomBarWhenPushed = YES;

    [self handleData];
    [self settingsNavigationController];

    
}
- (void)settingsNavigationController {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.tintColor = [UIColor blackColor];
    button.frame = CGRectMake(0, 0, 25, 25);
    [button setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
                                             
                                             
}
- (void)backAction:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.tableView registerClass:[EveryDayTableViewCell class] forCellReuseIdentifier:@"cell"];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arrayForCellData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EveryDayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return kHeight / 3;
    
}

//添加每个cell出现时的3D动画
-(void)tableView:(UITableView *)tableView willDisplayCell:(EveryDayTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //        CATransform3D rotation;//3D旋转
    //
    //        rotation = CATransform3DMakeTranslation(0 ,50 ,20);
    ////        rotation = CATransform3DMakeRotation( M_PI_4 , 0.0, 0.7, 0.4);
    //        //逆时针旋转
    //
    //        rotation = CATransform3DScale(rotation, 0.9, .9, 1);
    //
    //        rotation.m34 = 1.0/ -600;
    //
    //        cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    //        cell.layer.shadowOffset = CGSizeMake(10, 10);
    //        cell.alpha = 0;
    //
    //        cell.layer.transform = rotation;
    //
    //        [UIView beginAnimations:@"rotation" context:NULL];
    //        //旋转时间
    //        [UIView setAnimationDuration:0.6];
    //        cell.layer.transform = CATransform3DIdentity;
    //        cell.alpha = 1;
    //        cell.layer.shadowOffset = CGSizeMake(0, 0);
    //        [UIView commitAnimations];
    
    
    [cell cellOffset];
    
    EveryDayModel *model = [self.arrayForCellData objectAtIndex:indexPath.row];
    
    [cell setModel:model];
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self showImageAtIndexPath:indexPath];
}
#pragma mark - 设置详情界面
- (void)showImageAtIndexPath:(NSIndexPath *)indexPath{
    
    _array = self.arrayForCellData;
    
    _currentIndexPath = indexPath;
    
    EveryDayTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    CGRect rect = [cell convertRect:cell.bounds toView:nil];
    CGFloat y = rect.origin.y;
    
    _rilegoule = [[rilegouleView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) imageArray:_array index:indexPath.row];
    _rilegoule.offsetY = y;
    _rilegoule.animationTrans = cell.picture.transform;
    _rilegoule.animationView.picture.image = cell.picture.image;
    
    _rilegoule.scrollView.delegate = self;
    
    [[self.tableView superview] addSubview:_rilegoule];
    //[[NSNotificationCenter defaultCenter] postNotificationName:@"隐藏" object:nil];
    
    //添加轻扫手势
    UISwipeGestureRecognizer *Swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    
    _rilegoule.contentView.userInteractionEnabled = YES;
    
    Swipe.direction = UISwipeGestureRecognizerDirectionUp;
    
    [_rilegoule.contentView addGestureRecognizer:Swipe];
    
    //添加点击播放手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    [_rilegoule.scrollView addGestureRecognizer:tap];
    
    [_rilegoule aminmationShow];
    
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    
    //    if (self.viewForKVO != nil) {
    //        self.viewForKVO.frame = CGRectMake(self.view.frame.size.width / 5 * index, self.view.frame.size.height - 5, self.view.frame.size.width / 5, 5);
    //    }
    
    
    
    if ([scrollView isEqual:_rilegoule.scrollView]) {
        
        
        for (ImageContentView *subView in scrollView.subviews) {
            
            
            if ([subView respondsToSelector:@selector(imageOffset)] ) {
                [subView imageOffset];
            
            }
        
        }
        
        CGFloat x = _rilegoule.scrollView.contentOffset.x;
        
        CGFloat off = ABS( ((int)x % (int)kWidth) - kWidth/2) /(kWidth/2) + .2;
        
        [UIView animateWithDuration:1.0 animations:^{
            _rilegoule.playView.alpha = off;
            _rilegoule.contentView.titleLabel.alpha = off + 0.0;
            _rilegoule.contentView.littleLabel.alpha = off + 0.0;
            _rilegoule.contentView.lineView.alpha = off + 0.0;
            _rilegoule.contentView.descripLabel.alpha = off + 0.0;
            _rilegoule.contentView.collectionCustom.alpha = off + 0.0;
            _rilegoule.contentView.shareCustom.alpha = off + 0.0;
            _rilegoule.contentView.cacheCustom.alpha = off + 0.0;
            _rilegoule.contentView.replyCustom.alpha = off + 0.0;
            
        }];
        
    } else {
        
        NSArray<EveryDayTableViewCell *> *array = [self.tableView visibleCells];
        
        [array enumerateObjectsUsingBlock:^(EveryDayTableViewCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            [obj cellOffset];
        }];
        
    }

    
}
// 滑动将要结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    
    if ([scrollView isEqual:_rilegoule.scrollView]) {
        
        int index = floor((_rilegoule.scrollView.contentOffset.x - scrollView.frame.size.width / 2) / scrollView.frame.size.width) + 1;
        
       
        
        _rilegoule.scrollView.currentIndex = index;
        
        self.currentIndexPath = [NSIndexPath indexPathForRow:index inSection:self.currentIndexPath.section];
        
        [self.tableView scrollToRowAtIndexPath:self.currentIndexPath atScrollPosition:(UITableViewScrollPositionMiddle) animated:NO];
        
        [self.tableView setNeedsDisplay];
        EveryDayTableViewCell *cell = [self.tableView cellForRowAtIndexPath:self.currentIndexPath];
        
        [cell cellOffset];
        
        CGRect rect = [cell convertRect:cell.bounds toView:nil];
        _rilegoule.animationTrans = cell.picture.transform;
        _rilegoule.offsetY = rect.origin.y;
        
        EveryDayModel *model = _array[index];
        
        [_rilegoule.contentView setData:model];
        
        [_rilegoule.animationView.picture sd_setImageWithURL:[NSURL URLWithString:[model.cover objectForKey:@"feed"]] placeholderImage:nil];
        
    }
    
}

#pragma mark - 平移手势触发事件
- (void)panAction:(UISwipeGestureRecognizer *)swipe{
   // [[NSNotificationCenter defaultCenter] postNotificationName:@"显示" object:nil];
    [_rilegoule animationDismissUsingCompeteBlock:^{
        
        _rilegoule = nil;
      
    }];
    
}

#pragma mark - 点击手势
- (void)tapAction{
    
    
}
#pragma mark - 数据解析
- (void)handleData {
    self.arrayForCellData = [NSMutableArray array];
    self.arrayForCellData = [NSMutableArray arrayWithArray:[[DataBaseHandler shareDataBaseHandler] select]];
    
    if (self.arrayForCellData.count == 0) {

        self.image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
        [self.view addSubview:self.image];
        self.image.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2 - 50);
        self.image.image = [UIImage imageNamed:@"tishilian"];
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 40)];
        [self.view addSubview:self.label];
        self.label.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
        self.label.text = @"目前还没有任何收藏, 快去逛逛吧!";
        self.label.textAlignment = NSTextAlignmentCenter;
        
    }else{

        [self createTableView];
    }
    
    
    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
